home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Online / Socks5 / src / server / daemon.c next >
Encoding:
C/C++ Source or Header  |  1999-03-10  |  6.4 KB  |  209 lines

  1. /* Copyright (c) 1995-1999 NEC USA, Inc.  All rights reserved.               */
  2. /*                                                                           */
  3. /* The redistribution, use and modification in source or binary forms of     */
  4. /* this software is subject to the conditions set forth in the copyright     */
  5. /* document ("Copyright") included with this distribution.                   */
  6.  
  7. /*
  8.  * $Id: daemon.c,v 1.62.2.1.2.16 1999/03/05 21:52:47 steve Exp $
  9.  */
  10.  
  11. /* This file has the main function in it for socks5, as well as variables    */
  12. /* which most or several modules will use...                                 */
  13. #include "socks5p.h"
  14. #include "threads.h"
  15. #include "daemon.h"
  16. #include "socket.h"
  17. #include "protocol.h"
  18. #include "msgids.h"
  19. #include "log.h"
  20.  
  21.  
  22. #ifndef _DECTHREADS_
  23. #define MAXTHREADS 64
  24. #else
  25. #define MAXTHREADS 16
  26. #endif
  27.  
  28. #ifndef NUMCLIENTS
  29. #define NUMCLIENTS 64
  30. #endif
  31.  
  32. #ifndef HAVE_SETSID
  33. #ifdef  HAVE_SETPGID
  34. #define setsid() setpgid(0, getpid())
  35. #elif defined(SETPGRP_VOID)
  36. #define setsid() setpgrp()
  37. #else
  38. #define setsid() setpgrp(0, getpid())
  39. #endif
  40. #endif
  41.  
  42. MUTEX_T lt_mutex  = MUTEX_INITIALIZER; /* localtime mutex    */
  43. MUTEX_T accept_mutex  = MUTEX_INITIALIZER; /* localtime mutex    */
  44. MUTEX_T conn_mutex  = MUTEX_INITIALIZER; /* connection mutex    */
  45. MUTEX_T env_mutex = MUTEX_INITIALIZER; /* *env mutex         */
  46. MUTEX_T gpw_mutex = MUTEX_INITIALIZER; /* getpwd* mutex      */
  47. MUTEX_T gh_mutex  = MUTEX_INITIALIZER; /* gethost* mutex     */
  48. MUTEX_T gs_mutex  = MUTEX_INITIALIZER; /* getserv* mutex     */
  49.  
  50. int nthreads   = 0;
  51. int nservers   = 0;
  52. int isthreaded = 0;
  53. int servermode = 0;
  54. int idletimeout = 15;
  55. char *bindif   = NULL;
  56. u_short ludpport = 0;
  57. u_short hudpport = 0xffff;
  58.  
  59. static void version(void) {
  60.     fprintf(stdout, "Socks5 version: %s\n", SOCKS5_VERSION_NAME);
  61.     exit(0);
  62. }
  63.  
  64. static void usage(void) {
  65.     fprintf(stderr, "Usage incorrect...\n");
  66.     fprintf(stderr, "usage: socks5 ");
  67.     fprintf(stderr, "[-d [X]|--debug [X]] ");
  68.     fprintf(stderr, "[-s|--stderr] ");
  69.     fprintf(stderr, "[-v|--version] ");
  70.     fprintf(stderr, "\n");
  71.     fprintf(stderr, "[-b X|--bindintfc X] ");
  72.     fprintf(stderr, "[-f|--foreground] ");
  73.     fprintf(stderr, "[-i|--inetd] ");
  74.     fprintf(stderr, "[-p|--prefork] ");
  75.     fprintf(stderr, "[-o|--oneshot] ");
  76.     fprintf(stderr, "[-t|--threaded] ");
  77.     fprintf(stderr, "[-n X|--nchildren X]");
  78.     fprintf(stderr, "\n");
  79.     exit(-1);
  80. }
  81.  
  82. int main(int argc, char **argv, char **envp) {
  83.     char tbuf[1024];
  84.     int how = S5_LOG_SYSTEM, level = S5_LOG_INFO;
  85.     time_t now = time(NULL);
  86.     int separate = 1;
  87.  
  88.     IFTHREADED(MUTEX_SETUP(accept_mutex);)
  89.     IFTHREADED(MUTEX_SETUP(env_mutex);)
  90.     IFTHREADED(MUTEX_SETUP(gpw_mutex);)
  91.     IFTHREADED(MUTEX_SETUP(gh_mutex);)
  92.     IFTHREADED(MUTEX_SETUP(gs_mutex);)
  93.     IFTHREADED(MUTEX_SETUP(lt_mutex);)
  94.  
  95.     for (argc--, argv++; argc > 0; argc--, argv++) {
  96.     if (!strncmp(*argv, "-d", strlen("-d")) || !strncmp(*argv, "--debug", strlen("--debug")))  {
  97.             if (*(argv+1) && isdigit((int)**(argv+1))) {
  98.                 level = S5_LOG_DEBUG(atoi(*++argv)); argc--;
  99.             } else level = S5_LOG_DEBUG(25);
  100.     } else if (!strcmp(*argv, "-s") || !strcmp(*argv, "--stderr"))   {
  101.         how = S5_LOG_LOCAL;
  102.     } else if (!strcmp(*argv, "-i") || !strcmp(*argv, "--inetd"))  {
  103.         servermode = INETD;
  104.     } else if (!strcmp(*argv, "-f") || !strcmp(*argv, "--foreground")) {
  105.         separate = 0;
  106.     } else if (!strcmp(*argv, "-p") || !strcmp(*argv, "--prefork")) {
  107.         servermode = PREFORKING;
  108.     } else if (!strcmp(*argv, "-n") || !strcmp(*argv, "--nchildren")) {
  109.             if (!(*++argv) || !isdigit((unsigned char)**argv)) usage();
  110.             else {
  111.                 nservers = atoi(*argv); argc--;
  112.             }
  113.     } else if (!strcmp(*argv, "-b") || !strcmp(*argv, "--bindintfc")) {
  114.             if (!(*++argv)) usage();
  115.             else {
  116.                 bindif = strdup(*argv); argc--;
  117.             }
  118.     } else if (!strcmp(*argv, "-v") || !strcmp(*argv, "--version"))   {
  119.         version();
  120.     } else if (!strcmp(*argv, "-t") || !strcmp(*argv, "--threaded"))  {
  121.         S5LogShowThreadIDS = 1;
  122.         servermode = THREADED;
  123.     } else if (!strcmp(*argv, "-o") || !strcmp(*argv, "--oneshot"))   {
  124.         servermode = SINGLESHOT;
  125.         level      = S5_LOG_DEBUG_MAX;
  126.         how        = S5_LOG_LOCAL;
  127.         separate   = 0;
  128.     } else {
  129.         usage();
  130.     }
  131.     }
  132.  
  133.     if (separate && fork() > 0) exit(0);
  134.     chdir("/");
  135.     umask(0);
  136.     setsid();
  137.  
  138.     nthreads = MIN(MAXOPENS/4 - 1, MAXTHREADS) - 1;
  139.     if (nservers == 0) {
  140.     if (servermode == THREADED) nservers = NUMCLIENTS/4;
  141.     else nservers = MIN(MAXCLIENTS, NUMCLIENTS);
  142.     }
  143.  
  144. #ifdef RLIMIT_NOFILE
  145.     if (servermode == THREADED) {
  146.     struct rlimit rl;
  147.     
  148.         /* try allow for maxc open file descriptors                          */
  149.     if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
  150.         if (rl.rlim_cur < rl.rlim_max) {
  151.             rl.rlim_cur = rl.rlim_max;
  152. #ifndef bsdi
  153.             setrlimit(RLIMIT_NOFILE, &rl);
  154. #endif
  155.         }
  156.  
  157.         nthreads = MIN(rl.rlim_cur/4 - 1, MAXTHREADS) - 1;
  158.     }
  159.     }
  160. #endif
  161.  
  162. #ifdef RLIMIT_NPROC
  163.     if (servermode != INETD) {
  164.     struct rlimit rl;
  165.     
  166.     /* try allow for maxc children (or MAXCLIENTS)                       */
  167.     if (getrlimit(RLIMIT_NPROC, &rl) == 0 && nservers > rl.rlim_cur && nservers <= rl.rlim_max) {
  168.         rl.rlim_cur = nservers;
  169.         setrlimit(RLIMIT_NPROC, &rl);
  170.     }
  171.     }
  172. #endif
  173.  
  174.     S5LogStart(&S5LogDefaultHandle, how, level, "Socks5");
  175.  
  176.     /* make sure that number of child processes will not exceed the system    */
  177.     /* limit...                                                               */
  178.     if (nservers > MAXCLIENTS) {
  179.         S5LogUpdate(S5LogDefaultHandle, S5_LOG_INFO, MSGID_SERVER_START, "Requested child process number (%d). Socks5 will use the system limit (%d)", nservers, MAXCLIENTS);
  180.     nservers = MAXCLIENTS;
  181.     }
  182.  
  183.  
  184.     LIBPREFIX2(init)("Socks5");
  185.  
  186.     MUTEX_LOCK(lt_mutex);
  187.     strftime(tbuf, sizeof(tbuf), "%c", localtime(&now));
  188.     MUTEX_UNLOCK(lt_mutex);
  189.     switch (servermode) {
  190.     case NORMAL:
  191.         strcat(tbuf, " in normal mode");      break;
  192.     case INETD:
  193.         strcat(tbuf, " from inetd");          break;
  194.     case PREFORKING:
  195.         strcat(tbuf, " in preforking mode");  break;
  196.     case SINGLESHOT:
  197.         strcat(tbuf, " in single shot mode"); break;
  198.     case THREADED:
  199.         strcat(tbuf, " in threading mode");   break;
  200.     default:
  201.         strcat(tbuf, " in unknown mode");     break;
  202.     }
  203.     S5LogUpdate(S5LogDefaultHandle, S5_LOG_INFO, MSGID_SERVER_START, "Socks5 starting at %s", tbuf);
  204.  
  205.     GetConnection();
  206.     return(-1);
  207. }
  208.  
  209.